package com.chuangke.message.controller;

import com.chuangke.common.db.page.CkPageQuery;
import com.chuangke.common.http.HttpResult;
import com.chuangke.common.utils.SecurityUtils;
import com.chuangke.message.constant.MsgConstants;
import com.chuangke.message.dto.SendMessageDto;
import com.chuangke.message.service.SysMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("message")
public class SysMessageController {

	@Autowired
	private SysMessageService sysMessageService;

	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value = "/find")
	public HttpResult find(@RequestBody  Map<String,Object> params) {
		params.putAll((Map<? extends String, ?>) params.getOrDefault("param",new HashMap<>()));
		return HttpResult.ok(sysMessageService.find(new CkPageQuery(params)));
	}
	
	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value = "/send")
	public HttpResult send(@RequestBody  SendMessageDto msgDto) {
		sysMessageService.send(msgDto) ;
		return HttpResult.ok();
	}
	
	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping(value = "/unreadcount")
	public HttpResult findUnReadCount() {
		String userId = SecurityUtils.getUserId() ;
		return HttpResult.ok(sysMessageService.findUnReadCount(userId,MsgConstants.STATUS_UNREAD));
	}
	
	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value = "/markRead")
	public HttpResult markRead(@RequestBody List<String> list) {
		sysMessageService.markRead(list);
		return HttpResult.ok();
	}
	
	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value = "/markDone")
	public HttpResult markDone(@RequestBody List<String> list) {
		sysMessageService.markDone(list);
		return HttpResult.ok();
	}

}
